package com.company.sensortest;



import java.io.InputStream;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.opengl.GLSurfaceView.Renderer;
import android.os.SystemClock;
import android.util.Log;


public class CubeRenderer implements Renderer {
	
	private Cube mCube;
	private float mCubeRotAngle = 0;
	private int[] mTextures = new int[1];
	private Context mContext;	
	private int mFrames = 1;
	private long mStartTime;
	private final int SAMPLE_PERIOD_FRAMES = 10;
	private int mMsPerFrame;
	private int mPrevMsPerFrame;
	private final float SAMPLE_FACTOR = 1.0f / SAMPLE_PERIOD_FRAMES;	
	
	private float mOriention = 0.0f;
	
	private boolean bb = false;
	private boolean cc = false;
	
	public CubeRenderer(Context context) {	
		mContext = context;
		mCube = new Cube(1, 1, 1);     
	}

	@Override
	public void onDrawFrame(GL10 gl) {
		// Clears the screen and depth buffer.
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glTexEnvx(GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE,
                GL10.GL_MODULATE);				
		
		gl.glLoadIdentity(); 				

		GLU.gluLookAt(gl, 0, 0, 5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
		gl.glRotatef(-90, 0.0f, 0.0f, 1.0f);
		
		gl.glPushMatrix();
//		gl.glTranslatef(-2, 0, 0);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[0]);	
//		gl.glRotatef(-mOriention, 0.0f, 0.0f, 1.0f);							
		gl.glRotatef(mCubeRotAngle, 0.0f, 0.0f, 1.0f);
		mCube.draw(gl);
		gl.glPopMatrix();					
//		rotate CW
		if(cc == true){
			if(mCubeRotAngle == -90)
				mCubeRotAngle = -90;
			else	
				mCubeRotAngle -= 5;
		}
		
		checkMsPF();
	}

	private void checkMsPF() {
	    long time = SystemClock.uptimeMillis();
	    if(mStartTime == 0)
	        mStartTime = time;
	    
	    if(mFrames++ == SAMPLE_PERIOD_FRAMES){
	        mFrames = 1;
	        long delta = time - mStartTime;
	        mStartTime = time;
	        mMsPerFrame = (int) (delta * SAMPLE_FACTOR);	        
	    }
	    
	    if(mPrevMsPerFrame != mMsPerFrame){
	        mPrevMsPerFrame = mMsPerFrame;
	        Log.d("==== Ms / Frame ====", String.valueOf(mMsPerFrame));
	    }
	    
    }

    @Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		GLU.gluPerspective(gl, 45.0f, (float) width / (float) height, 0.1f,
				100.0f);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();

	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
	    
        gl.glGenTextures(1, mTextures, 0);       
		
        
		gl.glClearColor(0.3f, 0.3f, 0.3f, 0.0f);
		gl.glShadeModel(GL10.GL_SMOOTH);
		
		gl.glEnable(GL10.GL_TEXTURE_2D);
			
		gl.glClearDepthf(1.0f);
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glDepthFunc(GL10.GL_LEQUAL);
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
		
		gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextures[0]);
        
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);

        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT);
        gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT);
                
        InputStream is = mContext.getResources().openRawResource(R.drawable.nehe);
        Bitmap bitmap = BitmapFactory.decodeStream(is);
        
        GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0);

	}
	
	public void setOrientation(float orientation){
//		mOriention = orientation;
		if(bb == false){
			bb = true;
			cc = true;
		}
	}	
}
